package com.ruoyi.web.controller.module.event;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.event.EventPlanTaskLeader;
import com.ruoyi.system.service.module.event.IEventPlanService;
import com.ruoyi.system.service.module.event.IEventPlanTaskLeaderService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 计划任务的负责人  验收人Controller
 *
 * @author ruoyi
 * @date 2023-12-25
 */
@Validated
@Api(value = "计划任务的负责人  验收人控制器", tags = {"计划任务的负责人  验收人管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/event/task/leader")
public class EventPlanTaskLeaderController extends BaseController {

    private final IEventPlanTaskLeaderService eventPlanTaskLeaderService;
	private final IEventPlanService eventPlanService;
	private final ISysUserService sysUserService;
	private final IWxUserService wxUserService;

    /**
     * 查询计划任务的负责人  验收人列表
     */
    @ApiOperation("查询计划任务的负责人  验收人列表")
    @PreAuthorize("@ss.hasPermi('module:planTaskLeader:list')")
    @GetMapping("/list")
    public TableDataInfo<EventPlanTaskLeader> list(@Validated EventPlanTaskLeader eventPlanTaskLeader) {
        QueryWrapper<EventPlanTaskLeader> qw = new QueryWrapper<>();
		if(eventPlanTaskLeader.getEventPlanId() != null){
			qw.eq("event_plan_id", eventPlanTaskLeader.getEventPlanId());
		}
        TableDataInfo<EventPlanTaskLeader> td = PageUtils.buildDataInfo(eventPlanTaskLeaderService.page(PageUtils.buildPage(),qw));
        List<EventPlanTaskLeader> list = td.getRows();
		for (EventPlanTaskLeader leader : list){
			if (leader.getEventPlanId()!=null){
				leader.setEventPlanName(eventPlanService.findById(leader.getEventPlanId()).getPlanTitle());
			}
			if (leader.getLeaderSysUserId()!=null){
				leader.setNickName(sysUserService.getById(leader.getLeaderSysUserId()).getNickName());
			}
			if (leader.getLeaderWxUserId()!=null){
				leader.setWxUserName(wxUserService.findById(leader.getLeaderWxUserId()).getNickName());
			}else {
				leader.setWxUserName("未绑定");
			}
		}
        return td;
    }

    /**
     * 导出计划任务的负责人  验收人列表
     */
    @ApiOperation("导出计划任务的负责人  验收人列表")
    @PreAuthorize("@ss.hasPermi('module:planTaskLeader:export')")
    @Log(title = "计划任务的负责人  验收人", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EventPlanTaskLeader> export(@Validated EventPlanTaskLeader eventPlanTaskLeader) {
        List<EventPlanTaskLeader> list = eventPlanTaskLeaderService.list();
        ExcelUtil<EventPlanTaskLeader> util = new ExcelUtil<EventPlanTaskLeader>(EventPlanTaskLeader.class);
        return util.exportExcel(list, "计划任务的负责人  验收人");
    }

    /**
     * 获取计划任务的负责人  验收人详细信息
     */
    @ApiOperation("获取计划任务的负责人  验收人详细信息")
    @PreAuthorize("@ss.hasPermi('module:planTaskLeader:query')")
    @GetMapping("/{id}")
    public AjaxResult<EventPlanTaskLeader> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
        EventPlanTaskLeader eventPlanTaskLeader = eventPlanTaskLeaderService.getById(id);
        return AjaxResult.success(eventPlanTaskLeader);
    }

    /**
     * 新增计划任务的负责人  验收人
     */
    @ApiOperation("新增计划任务的负责人  验收人")
    @PreAuthorize("@ss.hasPermi('module:planTaskLeader:add')")
    @Log(title = "计划任务的负责人  验收人", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EventPlanTaskLeader eventPlanTaskLeader) {
        return toAjax(eventPlanTaskLeaderService.save(eventPlanTaskLeader) ? 1 : 0);
    }

    /**
     * 修改计划任务的负责人  验收人
     */
    @ApiOperation("修改计划任务的负责人  验收人")
    @PreAuthorize("@ss.hasPermi('module:planTaskLeader:edit')")
    @Log(title = "计划任务的负责人  验收人", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EventPlanTaskLeader eventPlanTaskLeader) {
        return toAjax(eventPlanTaskLeaderService.updateById(eventPlanTaskLeader) ? 1 : 0);
    }

    /**
     * 删除计划任务的负责人  验收人
     */
    @ApiOperation("删除计划任务的负责人  验收人")
    @PreAuthorize("@ss.hasPermi('module:planTaskLeader:remove')")
    @Log(title = "计划任务的负责人  验收人" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(eventPlanTaskLeaderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
